﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoachingOrganizer.Database.DataAccess;
using CoachingOrganizer.Toolkit.Events;
using CoachingOrganizer.ClientInfrastructure.ServicesEntities.LocalStorage.Tools.MoodLevel;

namespace CoachingOrganizer.Database.DataAccessLayer.Tools.MoodLevel
{
    public class MoodLevelDAL : ToolDALBase, IMoodLevelInfo
    {
        private readonly List<MoodValueDAL> _values = new List<MoodValueDAL>();
        private MoodLevelDA _da;
        public DateTime _start;
        public DateTime _finish;
        private TimeSpan _period;

        public DateTime Start
        {
            get { return _start; }
            set { _start = value; }
        }

        public DateTime Finish
        {
            get { return _finish; }
            set { _finish = value; }
        }

        public TimeSpan Period
        {
            get { return _period; }
            set { _period = value; }
        }

        public List<MoodValueDAL> Values
        {
            get { return _values; }
        }

        public IEnumerable<IMoodValueInfo> MoodValuesInfo
        {
            get { return _values; }
        }

        public MoodLevelDAL(DbManagerBase dbManager)
            : base(dbManager)
        {

        }

        public override object CreateNewDA(GoalDA goal)
        {
            return _da = new MoodLevelDA
            {
                GoalId = goal.Id,
                Start = _start,
                Finish = _finish,
                PeriodMinutes = _period.TotalMinutes
            };
        }

        public override ActionResult Save()
        {
            _da.Start = _start;
            _da.Finish = _finish;
            _da.PeriodMinutes = _period.TotalMinutes;
            return _dbManager.SafeRun(() => _dbManager.Context.InsertOrReplace(_da));
        }

        public bool Load(int id)
        {
            MoodLevelDA period = _dbManager.Context.Find<MoodLevelDA>(da => da.Id == id);
            if (period == null)
                return false;
            Load(period);
            return true;
        }

        public void Load(MoodLevelDA moodLevel)
        {
            _da = moodLevel;
            _start = moodLevel.Start;
            _finish = moodLevel.Finish;
            _period = TimeSpan.FromMinutes(moodLevel.PeriodMinutes);
            LoadValues();
        }

        public override ActionResult Remove()
        {
            if (_da != null)
                return _dbManager.SafeRun(() => _dbManager.Context.Delete<MoodLevelDA>(_da.Id));
            return ActionResult.GetErrorResult(new KeyNotFoundException());
        }

        public ActionResult<IMoodValueInfo> AddNewMoodValue(Action<IMoodValueInfo> initCallback)
        {
            MoodValueDAL newMoodValue = CreateNewValue();
            if (initCallback != null)
                initCallback(newMoodValue);
            ActionResult addResult = AddValue(newMoodValue);
            return addResult.IsValid ? ActionResult<IMoodValueInfo>.GetValidResult(newMoodValue) : ActionResult<IMoodValueInfo>.GetErrorResult(addResult);
        }

        public MoodValueDAL CreateNewValue()
        {
            return new MoodValueDAL(_dbManager);
        }

        public ActionResult AddValue(MoodValueDAL moodValue)
        {
            _values.Add(moodValue);
            return _dbManager.SafeRun(() => _dbManager.Context.Insert(moodValue.CreateNewDA(_da)));
        }

        public ActionResult LoadValues()
        {
            return _dbManager.SafeRun(() =>
            {
                foreach (MoodValueDA moodValueDA in _dbManager.Context.Table<MoodValueDA>().Where(g => g.MoodLevelId == _da.Id).ToArray())
                {
                    MoodValueDAL moodValue = new MoodValueDAL(_dbManager);
                    moodValue.Load(moodValueDA);
                    _values.Add(moodValue);
                }
            });
        }
    }
}